package com.pignest.hotel.system;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pignest.framework.util.Log;
import com.pignest.hotel.annotation.Configuration;
import com.pignest.hotel.beans.Dictionary;
import com.pignest.hotel.service.DictionaryService;
import com.pignest.hotel.spring.Services;

/**
 * 部分字典表设置值/默认值读取
 * */
@SuppressWarnings("unused")
public class Config {
	
	@Configuration(key=Key.HOTEL_NAME, desc="酒店名称")
	public static String getHotelName() {
		return getString(Key.HOTEL_NAME, "7号生态酒店");
	}
	
	/**
	 * 门卡相关配置
	 * {Name:'',Key:'',Port:'',Reader:'',Type:'',Sector:'',Block:''}
	 * <pre>
	 * Port:	com1  1  com2 2  com3  3  com4 4
	 * Reader:	BTEM232 1  BTIB232 2
	 * Type:	EM4150 0    EM4469 1
	 * Sector:	EM4150 0  Sector 3 , EM4469 1  Sector 5
	 * Block:	
	 * </pre>
	 * */
	@Configuration(key=Key.CARD_CONFIG, desc="门卡配置")
	public static String getCardConfig() {
		return getString(Key.CARD_CONFIG, "{Name:'BTLOCK56',Key:'11111111',Port:'1',Reader:'2',Type:'1',Sector:'5',Block:'0'}");
	}
	
	@Configuration(key=Key.DEFAULT_PASSWORD, desc="员工默认登录密码")
	public static String getDefaultPassword() {
		return getString(Key.DEFAULT_PASSWORD, "7hotel");
	}
	
	/** @return "1" */
	public static String getSuperUserWorkNumber() {
		return "1";
	}
	
	/** 酒店电话 */
	@Configuration(key=Key.HOTEL_PHONE, desc="酒店电话号码")
	public static String getHotelPhoneNumber() {
		return getString(Key.HOTEL_PHONE, "(0594)5555777");
	}
	
//	/** 预订房间的默认的保留时间, 单位小时 */
//	@Configuration(key=Key.BOOKING_KEEP_HOURS, desc="预订房间的默认的保留时间, 单位小时(1-72)")
//	public static int getBookingKeepHours() {
//		return getInteger(Key.BOOKING_KEEP_HOURS, 1, 72, 6);
//	}
	
	public static String getSelfServiceImagePath() {
		String path = getString(Key.HOTEL_SHOWING_IMAGE_PATH);
		if (path != null && new File(path).exists())
			return path;
		return null;
	}
	
	@Configuration(key=Key.LINE_CAPACITY_SELF_SERVICE, desc="自助系统中选房时每行显示的房间数量(1-30)")
	public static int getLineCapacityOfSelfService() {
		return getInteger(Key.LINE_CAPACITY_SELF_SERVICE, 1, 30, 7);
	}
	
	@Configuration(key=Key.LINE_CAPACITY_BOOKING, desc="预订的房态图中, 每行显示的房间数量(1-30)")
	public static int getLineCapacityOfBooking() {
		return getInteger(Key.LINE_CAPACITY_BOOKING, 1, 30, 10);
	}
	
	@Configuration(key=Key.LINE_CAPACITY_RECEPTION, desc="接待的房态图中, 每行显示的房间数量(1-30)")
	public static int getLineCapacityOfReception() {
		return getInteger(Key.LINE_CAPACITY_RECEPTION, 1, 30, 10);
	}
	
	/**
	 * 早上时间点
	 */
	@Configuration(key=Key.TIME_POINT_MORNING, desc="早上时间点(0-23)")
	public static int getMorningTimePoint() {
		return getInteger(Key.TIME_POINT_MORNING, 0, 23, 6);
	}
	
	/**
	 * 中午时间点
	 * */
	@Configuration(key=Key.TIME_POINT_NOON, desc="中午时间点(0-23)")
	public static int getNoonTimePoint() {
		return getInteger(Key.TIME_POINT_NOON, 0, 23, 14);
	}

	/**
	 * 晚上时间点
	 * */
	@Configuration(key=Key.TIME_POINT_EVENING, desc="晚上时间点(0-23)")
	public static int getEveningTimePoint() {
		return getInteger(Key.TIME_POINT_EVENING, 0, 23, 19);
	}
	
//	@Configuration(key=Key.MANUAL_NIGHT_AUDIT, desc="将否手动夜审(true/false)")
	public static boolean isManualNightAudit() {
		return getBoolean(Key.MANUAL_NIGHT_AUDIT, false);
	}
	
	/**
	 * 夜审时间, hh:mm
	 * */
	@Configuration(key=Key.NIGHT_AUDIT_TIME, desc="夜审时间")
	public static String getNightAuditTime() {
		return getTime(Key.NIGHT_AUDIT_TIME, "03:00");
	}
	
	/** 钟点房的计时周期 */
	@Configuration(key=Key.HOUR_PERIOD, desc="钟点房计时周期(1-12小时)")
	public static int getHourPeriod() {
		return getInteger(Key.HOUR_PERIOD, 1, 12, 3);
	}
	
	@Configuration(key=Key.HALF_DAY_PERIOD, desc="半天房计时周期(3-12小时)")
	public static int getHalfDayPeriod() {
		return getInteger(Key.HALF_DAY_PERIOD, 3, 12, 6);
	}
	
	@Configuration(key=Key.FLOORS, desc="房间楼层", single=false)
	public static List<String> getFloors() {
		List<String> r = Hotel.listFloors();
		if (r == null)
			r = new ArrayList<String>();
		return r;
	}
	
	@Configuration(key=Key.CANCEL_LIVING_MINUTES, desc="多少分钟不能撤单")
	public static int getCancelLivingMinutes() {
		return getInteger(Key.CANCEL_LIVING_MINUTES, 10, 120, 60);
	}
	
	@Configuration(key=Key.MIN_NIGHT_AUDIT_INTERVAL, desc="最短夜审间隔(小时, 12-24)")
	public static int getMinNightAuditInterval() {
		return getInteger(Key.MIN_NIGHT_AUDIT_INTERVAL, 12, 24, 23);
	}
	
	@Configuration(key=Key.DEFAULT_DEPOSIT, desc="房间的默认压金(单位:分)")
	public static int getDefaultDeposit() {
		return getInteger(Key.DEFAULT_DEPOSIT, 40000);
	}
	@Configuration(key=Key.DEFAULT_BILL_TEMPLATES,desc="票据的默认模板(1为条型票据,2为宽型票据)")
	public static int getDefaultBillTemplates(){
		return getInteger(Key.DEFAULT_BILL_TEMPLATES, 1, 2, 1);
	}
	///////////////////////////////////////////////////////////
	
	/** 控制台的日志等级 */
	public static int getConsoleLogLevel() {
		return getInteger(Key.CONSOLE_LOG_LEVEL, Log.LOG_LEVEL_NONE);
	}
	
	public static class Saver {
		public static void setConsoleLogLevel(int level) {
			save(Key.CONSOLE_LOG_LEVEL, level);
		}
		
		private static void save(String key, String value) {
			DictionaryService service = getService();
			service.saveDictionary(key, value);
		}
		private static void save(String key, int value) {
			save(key, String.valueOf(value));
		}
	}

	///////////////////////////////////////////////////////////
	
	private static boolean isHourMinite(String time) {
		return time.matches("\\d{1,2}:\\d{1,2}");
	}
	
	private static String getTime(String key, String def) {
		String t = getString(key);
		if (t != null && t.matches("[012]?\\d:[0-5]?\\d"))
			return t;
		return def;
	}
	
	private static int getInteger(String key, int min, int max, int def) {
		try {
			int r = Integer.parseInt(getString(key));
			if (r > max || r < min)
				return def;
			return r;
		} catch (Exception e) {
		}
		return def;
	}
	private static int getInteger(String key, int def) {
		try {
			return Integer.parseInt(getString(key));
		} catch (Exception e) {
		}
		return def;
	}
	
	private static int getInteger(String key) {
		return getInteger(key, 0);
	}
	
	private static String getString(String key, String def) {
		String v = null;
		try {	//TODO
			v = getService().getValue(key, null);
		} catch (Exception e) {
		}
		if (StringUtils.isEmpty(v))
			return def;
		return v;
	}
	
	private static String getString(String key) {
		return getString(key, null);
	}
	
	private static DictionaryService getService() {
		return Services.getDictionaryService();
	}
	
	private static boolean getBoolean(String key) {
		return getBoolean(key, false);
	}
	
	private static boolean getBoolean(String key, boolean def) {
		String v = getString(key);
		return def ? !"false".equals(v) : "true".equals(v);
	}
	
	////////////////////////////////////////////////////////////
	
	private Config() {
	}
}
